package com.example.autopia.view.Adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.autopia.databinding.ViewholderColorItemBinding
import com.example.autopia.view.Listeners.ColorItemListener

class ColorItemAdapter :
    RecyclerView.Adapter<ColorItemAdapter.ViewHolder>() {

    private lateinit var list : MutableList<String>
    private lateinit var listener: ColorItemListener

    fun attachListener(listener: ColorItemListener) {
        this.listener = listener
    }

    fun setData(newList : List<String>){
        list = newList.toMutableList()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ViewholderColorItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val news = list[position]
        holder.bind(news,listener)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(private val binding: ViewholderColorItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(color : String,listener: ColorItemListener) {
            binding.tvColor.text = color
            binding.btColor.setOnClickListener {
                listener.onColorItemClicked(color)
            }
        }
    }
}